package com.example.mybatisplusdemo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.mapper.PointsMallMapper;
import com.example.mybatisplusdemo.model.domain.PointsMall;
import com.example.mybatisplusdemo.model.domain.Product;
import com.example.mybatisplusdemo.mapper.ProductMapper;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.dto.ProductWithPointsDTO;
import com.example.mybatisplusdemo.service.IPointsMallService;
import com.example.mybatisplusdemo.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 存储商品详情 服务实现类
 * </p>
 *
 * @author lqj
 * @since 2025-06-24
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    private ProductMapper productMapper;

    @Autowired  // ✅ 一定要加上
    private PointsMallMapper pointsMallMapper;

    public ProductServiceImpl(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    @Override
    public Page<Product> pageList(Product product, PageDTO pageDTO) {
        Page<Product> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = productMapper.pageList(page,product);
        return page;
    }

    @Override
    public int logicDeleteById(Long id) {
        return productMapper.logicDeleteById(id);
    }

    @Override
    @Transactional
    public void saveProductWithPoints(ProductWithPointsDTO dto) {
        Product product = dto.getProduct();

        // 保存或更新商品
        if (product.getId() == null) {
            productMapper.insert(product);
        } else {
            productMapper.updateById(product);
        }

        // 积分商城相关逻辑
        if (Boolean.TRUE.equals(product.getPointsMall())) {
            PointsMall pointsMall = pointsMallMapper.selectByProductId(product.getId());

            if (pointsMall == null) {
                // 新增
                pointsMall = new PointsMall();
                pointsMall.setProductId(product.getId());
                pointsMall.setPointsPrice(dto.getPointsPrice());
                pointsMallMapper.insertPointsMall(pointsMall);
            } else {
                // 更新
                pointsMall.setPointsPrice(dto.getPointsPrice());
                pointsMallMapper.updatePointsMall(pointsMall);
            }

            // 更新 product 表中 pointsId 字段
            productMapper.updatePointsId(product.getId(), pointsMall.getId());
        }
    }

    @Override
    public ProductWithPointsDTO getPointsPriceByProductId(Long productId) {
        Product product = this.getById(productId);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }

        Integer pointsPrice = pointsMallMapper.selectPointsPriceByProductId(productId);

        ProductWithPointsDTO dto = new ProductWithPointsDTO();
        dto.setProduct(product);
        dto.setPointsPrice(pointsPrice);

        return dto;
    }

    @Override
    public Page<Product> pageListWithSort(Product product, PageDTO pageDTO, String orderBy, String sort) {
        Page<Product> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("product", product);
        paramMap.put("orderBy", orderBy);
        paramMap.put("sort", sort);
        paramMap.put("pointsPrice",product.pointsPrice);

        return productMapper.pageListWithSort(page, paramMap);
    }



}
